import XlsxPopulate from 'xlsx-populate';

export function downloadExcel (headfather = [], head = [], table = [], title = '') {
	if (table.length == 1) {
		this.$message.error('生成失败！数据为空');
		return;
	}
	var letter = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
		"T", "U", "V", "W", "X", "Y", "Z", "AA","AB","AC","AD","AE","AF","AG","AH","AI","AJ","AK","AL","AM","AN",
        "AO","AP","AQ","AR","AS","AT","AU","AV","AW","AX","AY","AZ","BA","BB","BC","BD","BE","BF","BG","BH","BI",
        "BJ","BK","BL","BM","BN","BO","BP","BQ","BR","BS","BT","BU","BV","BW","BX","BY","BZ","CA","CB","CC","CD",
		"CE","CF","CG","CH","CI","CJ","CK","CL","CM","CN","CO","CP","CQ","CR","CS","CT","CU","CV","CW","CX","CY",
		"CZ","DA","DB","DC","DD","DE","DF","DG","DH","DI","DJ","DK","DL","DM","DN","DO","DP","DQ","DR","DS","DT",
    "DU","DV","DW","DX","DY","DZ","EA","EB","EC","ED","EE","EF","EG","EH","EI","EJ","EK","EL","EM","EN","EO",
    "EP","EQ","ER","ES","ET","EU","EV","EW","EX","EY","EZ",
	];

	XlsxPopulate.fromBlankAsync()
		.then(workbook => {
			// 第一个sheet页
			let wb = workbook.sheet("Sheet1");
			// 设置列宽
			for(let i = 0; i<letter;i++){
				wb.column(letter[i]).width(200);
			}

			// 设置行高
			wb.row(1).height(30);
			// wb.row(4).height(50);
			// wb.row(listLen+6).height(50);
			// 标题合并单元格  A列到L列,1行到2行合并
			wb.range(`${letter[0]}1:${letter[headfather[headfather.length-1].length+1]}1`).merged(true)
				.value(title)
				.style("verticalAlignment", "center") //垂直居中
				.style("horizontalAlignment", "center") //水平位置
				.style("border", true)
				.style("fontSize", 18);
			// 设置第二列
			wb.range('A2:A3').merged(true)
				.value("机构")
				.style("verticalAlignment", "center") //垂直居中
				.style("horizontalAlignment", "cneter") //水平位置
				.style("border", true);
			headfather.forEach((item,index)=>{
				/*初始位*/
				if(index===0){
					wb.range(`${letter[1]}2:${letter[item.length]}2`).merged(true)
					.value(`${item.label}`)
					.style("verticalAlignment", "center") //垂直居中
					.style("horizontalAlignment", "center") //水平位置
					.style("border", true);
				}else{
					wb.range(`${letter[headfather[index-1].length+1]}2:${letter[item.length]}2`).merged(true)
					.value(`${item.label}`)
					.style("verticalAlignment", "center") //垂直居中
					.style("horizontalAlignment", "center") //水平位置
					.style("border", true);
				}
			})
			wb.range(`${letter[headfather[headfather.length-1].length+1]}2:${letter[headfather[headfather.length-1].length+1]}3`).merged(true)
				.value("总分")
				.style("verticalAlignment", "center") //垂直居中
				.style("horizontalAlignment", "center") //水平位置
				.style("wrapText", true) //自动换行
				.style("border", true);
			// wb.range('B2:T2').merged(true)
			// 	.value("签约服务质量")
			// 	.style("verticalAlignment", "center") //垂直居中
			// 	.style("horizontalAlignment", "center") //水平位置
			// 	.style("border", true);
			// wb.range('U2:BF2').merged(true)
			// 	.value("基本公共卫生服务质量")
			// 	.style("verticalAlignment", "center") //垂直居中
			// 	.style("horizontalAlignment", "center") //水平位置
			// 	.style("wrapText", true) //自动换行
			// 	.style("border", true);
            // wb.range('BG2:BS2').merged(true)
			// 	.value("医疗服务质量")
			// 	.style("verticalAlignment", "center") //垂直居中
			// 	.style("horizontalAlignment", "center") //水平位置
			// 	.style("wrapText", true) //自动换行
			// 	.style("border", true);
            // wb.range('BT2:BW2').merged(true)
			// 	.value("服务对象满意度")
			// 	.style("verticalAlignment", "center") //垂直居中
			// 	.style("horizontalAlignment", "center") //水平位置
			// 	.style("wrapText", true) //自动换行
			// 	.style("border", true);
            // wb.range('BX2:CB2').merged(true)
			// 	.value("效率得分")
			// 	.style("verticalAlignment", "center") //垂直居中
			// 	.style("horizontalAlignment", "center") //水平位置
			// 	.style("wrapText", true) //自动换行
			// 	.style("border", true);

			for (let i = 0; i < head.length; i++) {
                        // 表头
				        wb.cell(`${letter[i+1]+'3'}`)
                        .value(`${head[i]}`)
                        .style("border", true)
                        .style("verticalAlignment", "center") //垂直居中
                        .style("horizontalAlignment", "center"); //水平位置
            }

            table.map((item, index) => {
                let num = 0
                item.forEach((child)=>{
                    wb.cell(`${letter[num]}${index + 4}`)
                    .value(`${child}`)
                    .style('border', true)
                    .style('verticalAlignment', 'center') // 垂直居中
                    .style('horizontalAlignment', 'center') // 水平位置;
                    num++
                })
            })

			// Write to file.
			workbook.outputAsync().then(function(blob) {
				if (window.navigator && window.navigator.msSaveOrOpenBlob) {
					// If IE, you must uses a different method.
					window.navigator.msSaveOrOpenBlob(blob, title + '.xlsx');
				} else {
					let url = window.URL.createObjectURL(blob);
					let a = document.createElement('a');
					document.body.appendChild(a);
					a.href = url;
					a.download = title + '.xlsx';
					a.click();
					window.URL.revokeObjectURL(url);
					document.body.removeChild(a);
				}
			})
		});
}
